package com.rover022.flixelgame.manager
{
	import com.rover022.flixelgame.GameConfig;
	import com.rover022.flixelgame.element.BaseElement;
	import com.rover022.flixelgame.vo.HeroVo;
	import com.rover022.flixelgame.vo.ItemVo;
	import com.rover022.flixelgame.vo.UserVo;
	import com.rover022.flixelgame.world.BattleState;
	import com.rover022.flixelgame.world.displayobject.FlxLoadImage;
	import flash.utils.Dictionary;
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxRect;
	import org.flixel.FlxU;
	import org.flixel.plugin.photonstorm.FlxControl;
	import org.flixel.plugin.photonstorm.FlxControlHandler;

	/**
	 * 角色管理员
	 * 1提供角色初始创建...
	 * 2网络模式中多角色查找...
	 * 3战斗场中角色查找
	 * @author flashpig
	 *
	 */
	public class PlayerManager
	{
		private static var _instance:PlayerManager;
		/**
		 * 玩家自己的vo数据
		 */
		public var owerUserVo:UserVo = new UserVo();
		/**
		 * 队伍编组
		 */
		public var teamDict:Dictionary = new Dictionary();

		public function PlayerManager()
		{
			teamDict["team0"] = [];
			teamDict["team1"] = [];
			teamDict["team2"] = [];
		}

		public static function getInstance():PlayerManager
		{
			if (null == _instance)
			{
				_instance = new PlayerManager();
			}
			return _instance;
		}

		/**
		 * 取得当前游戏场景中  factionType 相同的对象数组
		 * @param factionType
		 *
		 */
		public function getOpponents(factionType:uint):Array
		{
			var _arr:Array = [];
			for (var i:int = 0; i < getCurrentBattle().playLay.members.length; i++)
			{
				var obj:BaseElement = getCurrentBattle().playLay.members[i];
				if (obj.factionType == factionType)
				{
					_arr.push(obj);
				}
			}
			return _arr;
		}

		/**
		 * 取得当前游戏场景中  factionType 不相同的对象数组
		 * @param factionType
		 *
		 */
		public function getOpponentsExcept(factionType:uint):Array
		{
			var _arr:Array = [];
			for (var i:int = 0; i < getCurrentBattle().playLay.members.length; i++)
			{
				var obj:BaseElement = getCurrentBattle().playLay.members[i];
				if (obj && obj.factionType != factionType)
				{
					_arr.push(obj);
				}
				else
				{
				}
			}
			return _arr;
		}

		/**
		 * 得到一个当前战场
		 * @return
		 *
		 */
		public function getCurrentBattle():BattleState
		{
			return FlxG.state as BattleState;
		}

		/**
		 * 计算两个对手直接的绝对值距离
		 * @param parent
		 * @param _opponent
		 * @param calculationMode
		 * @return
		 *
		 */
		public function getDistance(parent:BaseElement, _opponent:BaseElement, calculationMode:uint):Number
		{
			if (calculationMode == 0)
			{
				return FlxU.getDistance(parent.bodyElement.getMidpoint(), _opponent.bodyElement.getMidpoint());
			}
			else if (calculationMode == 1)
			{
				return FlxU.abs(parent.bodyElement.getMidpoint().x - _opponent.bodyElement.getMidpoint().x);
			}
			else if (calculationMode == 2)
			{
				return FlxU.abs(parent.bodyElement.getMidpoint().y - _opponent.bodyElement.getMidpoint().y);
			}
			else
			{
				throw(new Error("计算距离的方式有错误"));
			}
			return 0;
		}

		/**
		 * 移动一个角色向另外一个角色靠近
		 * @param parent
		 * @param param1
		 * @param calculationMode
		 *
		 */
		public function moveTo(parent:BaseElement, param1:BaseElement, calculationMode:uint):void
		{
			if (calculationMode == 0)
			{
				//未实现
			}
			else if (calculationMode == 1)
			{
				if (parent.bodyElement.x > param1.bodyElement.x)
				{
					param1.direction = -1; //左边移动
				}
				else
				{
					param1.direction = 1; //右边移动
				}
			}
			else if (calculationMode == 2)
			{
				//未实现
			}
		}

		/**
		 * 检测 是否有对手在自己的攻击距离之内 如果有 返回这个对手
		 * @param parent
		 * @param _players
		 * @param calculationMode
		 * @return
		 *
		 */
		public function InAttackArea(parent:BaseElement, _players:Array, calculationMode:uint):BaseElement
		{
			for (var i:int = 0; i < _players.length; i++)
			{
				var _opponent:BaseElement = _players[i] as BaseElement;
				var _leng:Number = PlayerManager.getInstance().getDistance(parent, _opponent, calculationMode);
				if (_leng < parent.attackDistance)
				{
					return _opponent;
				}
			}
			return null;
		}

		public function getUserVo():UserVo
		{
			return owerUserVo;
		}

		/**
		 * 获取制定id的hero
		 * @return
		 *
		 */
		public function getHeroById(id:String):HeroVo
		{
			for (var i:int = 0; i < owerUserVo.heros.length; i++)
			{
				var hero:HeroVo = owerUserVo.heros[i];
				if (hero.id == id)
				{
					return hero;
				}
			}
			return null;
		}

		/**
		 * 获取所有的英雄
		 * @return
		 *
		 */
		public function getAllHeros():Array
		{
			// TODO Auto Generated method stub
			return owerUserVo.heros;
		}

		/**
		 * 获取当前队伍
		 * @return
		 *
		 */
		public function getTeamByName(teamName:String = "team0"):Array
		{
			return teamDict[teamName];
		}

		/**
		 * 生产英雄
		 * @param stageRect
		 * @return
		 *
		 */
		public function makeControlHero(stageRect:FlxRect, controlRect:FlxRect):BaseElement
		{
			var hero:BaseElement = new BaseElement();
			if (FlxG.getPlugin(FlxControl) == null)
			{
				FlxG.addPlugin(new FlxControl);
			}
			FlxControl.create(hero.bodyElement, FlxControlHandler.MOVEMENT_INSTANT, FlxControlHandler.STOPPING_INSTANT, 1, false, false);
			FlxControl.player1.setMovementSpeed(200, 0, 200, 200);
			FlxControl.player1.setBounds(0, 0, controlRect.width, controlRect.height);
			FlxControl.player1.setWASDControl(false, false, true, true);
			FlxControl.player1.setJumpButton("SPACE", FlxControlHandler.KEYMODE_PRESSED, 200, FlxObject.FLOOR, 250, 200);
			FlxG.camera.follow(hero.bodyElement);
			FlxG.camera.bounds = stageRect;
			return hero;
		}

		/**
		 * 制作NPC 小人
		 * @param _x 坐标
		 * @param _y 坐标
		 * @param _name 名字
		 * @param imageId 图片ID
		 * @param functionType 功能
		 * @return
		 *
		 */
		public function makeNpc(vars:Object, functionType:String = ""):BaseElement
		{
			var npc:BaseElement = new BaseElement(0, vars);
			return npc;
		}

		/**
		 * 制作加载图片型FlxSprite
		 * @param imageID
		 * @return
		 *
		 */
		public function makeFlxLoadImage(imageID:String):FlxLoadImage
		{
			var sprite1:FlxLoadImage = new FlxLoadImage();
			sprite1.setImageUrl(GameConfig.webroot + imageID);
			return sprite1;
		}

		/**
		 * 制作战斗角色
		 * @param param0
		 * @return
		 *
		 */
		public function makeFighter(vars:Object = null):BaseElement
		{
			var fighter:BaseElement = new BaseElement(0, vars);
			return fighter;
		}

		/**
		 * 获取角色的所有道具
		 * @return
		 *
		 */
		public function getAllItems():Array
		{
			return owerUserVo.items;
		}

		//###############################################
		//             pvp  and pve
		//###############################################
		/**
		 * 获取pve战斗阵形 hero 数组
		 * @return
		 *
		 */
		public function getPveTeamHeros():Array
		{
			var array:Array = owerUserVo.pveTeam;
			var team:Array = [];
			for (var i:int = 0; i < array.length; i++)
			{
				var hero:HeroVo = getHeroById(array[i]);
				team.push(hero);
			}
			return team;
		}

		/**
		 * 获取pvp战斗阵形 hero 数组
		 * @return
		 *
		 */
		public function getPvpTeamHeros():Array
		{
			var array:Array = owerUserVo.pvpTeam;
			var team:Array = [];
			for (var i:int = 0; i < array.length; i++)
			{
				var hero:HeroVo = getHeroById(array[i]);
				team.push(hero);
			}
			return team;
		}

		/**
		 * 获取pvp战斗阵形 id 数组
		 * @return
		 *
		 */
		public function getPvpTeam():Array
		{
			return owerUserVo.pveTeam;
		}

		/**
		 * 获取pve战斗阵形
		 * @return
		 *
		 */
		public function getPveTeam():Array
		{
			return owerUserVo.pveTeam;
		}

		public function getGroceryGoods():Array
		{
			return owerUserVo.groceryGoods;
		}

		/**
		 * 根据这个id创建一个hero
		 * 如果已经有了 不做更改
		 *
		 */
		public function addHeroByID(id:String):HeroVo
		{
			var hero:HeroVo = this.getHeroById(id);
			if (hero == null)
			{
				ItemManager.getInstance().getItemFromConfig(id);
				var newHero:HeroVo = this.getHeroFromConfig(id);
				PlayerManager.getInstance().owerUserVo.heros.push(newHero);
				return newHero;
			}
			else
			{
				trace("已经有这个hero 添加失败")
				return hero;
			}
		}

		/**
		 * 从配置档案里面取一个道具的样本出来
		 *
		 */
		private function getHeroFromConfig(id:String):HeroVo
		{
			for (var i:int = 0; i < GameConfig.heroData.RECORD.length(); i++)
			{
				var tempid:String = GameConfig.heroData.RECORD[i].@id;
				if (tempid == id)
				{
					var itemvo:HeroVo = new HeroVo();
					itemvo.id = GameConfig.heroData.RECORD[i].@id;
					itemvo.name = GameConfig.heroData.RECORD[i].@name;
					itemvo.imageUrl = GameConfig.heroData.RECORD[i].@imageUrl;
					return itemvo;
				}
			}
			return null;
		}

		/**
		 * 删除英雄 
		 * @param id
		 * @return 
		 * 
		 */		
		public function removeHeroByID(id:String):Boolean
		{
			for (var i:int = 0; i < owerUserVo.heros.length; i++)
			{
				var hero:HeroVo = owerUserVo.heros[i];
				if (hero.id == id)
				{
					owerUserVo.heros.splice(i, 1);
					return true;
				}
			}
			return false;
		}
	}
}